package com.appkudos.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.appkudos.model.Recommendation;

public class RecController extends Controller {
    String response = "";
	String output = "";
	ArrayList<NameValuePair> nameValuePairs;
	
	/***
	 * Send a recommendation with the message from the sender to its recipients
	 * @param String username
	 * @param String message
	 * @param String appid
	 * @param String recipient
	 * @param boolean isGroupRecipient
	 * @return true if successful operation
	 */
	public boolean addRecommendation(String username, String message, String appid, 
			String recipient, boolean isGroupRecipient) {
			String isGroup = "";
			if(isGroupRecipient)
				isGroup = "1";
			else
				isGroup = "0";    
			nameValuePairs = new ArrayList<NameValuePair>();
		    nameValuePairs.add(new BasicNameValuePair("function","addRecommendation"));
		    nameValuePairs.add(new BasicNameValuePair("username",username));
		    nameValuePairs.add(new BasicNameValuePair("message",message));
		    nameValuePairs.add(new BasicNameValuePair("appid",appid));
		    nameValuePairs.add(new BasicNameValuePair("recipient",recipient));
		    nameValuePairs.add(new BasicNameValuePair("isGroupReceipient",isGroup));
		    
		    response = sendData(URL_REC_QUERIES, nameValuePairs);
		    
		    return executeOperation(response);
	}
	
	/***
	 * Retrieves all recommendations made to all friends of username and in all apps
	 * @param String username
	 * @return List<Recommendation>
	 */
	
	public Recommendation getRecommendation(String recid) {
		nameValuePairs = new ArrayList<NameValuePair>();
	    nameValuePairs.add(new BasicNameValuePair("function","retrieveRec"));
	    nameValuePairs.add(new BasicNameValuePair("recid",recid));
	    
	    response = sendData(URL_REC_QUERIES, nameValuePairs);
	    Recommendation returnRec = null;
	    
	    //parse json data
	    try{
	    	JSONArray jArray = new JSONArray("["+response+"]");
    		for(int i=0;i<jArray.length();i++){
            	JSONObject json_data = jArray.getJSONObject(i);
            	if(json_data.has("ERROR")){
            		//output += json_data.getString("ERROR");
            	}else{
            		returnRec = new Recommendation(json_data.getString("recid"), 
            				json_data.getString("message"), json_data.getString("sender"),
            				json_data.getString("recipient"), json_data.getString("dateSent"), 
            				json_data.getString("isGroupReceipient").equals("1"), json_data.getString("appid"));
	            }
            }
	    }catch(JSONException e){
	    		
	    }
	   	  
	    return returnRec;
	}
	
	/***
	 * Gets all recs sent about this app
	 * @param appid
	 * @return List<Rec> for recommendations sent about this app
	 */
	public List<Recommendation> getRecsForApp(String appid) {
		    nameValuePairs = new ArrayList<NameValuePair>();
		    nameValuePairs.add(new BasicNameValuePair("function","getRecsForApp"));
		    nameValuePairs.add(new BasicNameValuePair("appid",appid));
		    
		    response = sendData(URL_REC_QUERIES, nameValuePairs);
		    List<Recommendation> returnRecs = new ArrayList<Recommendation>();
		    
		    //parse json data
		    try{
		    	JSONArray jArray = new JSONArray("["+response+"]");
	    		for(int i=0;i<jArray.length();i++){
	            	JSONObject json_data = jArray.getJSONObject(i);
	            	if(json_data.has("ERROR")){
	            		//output += json_data.getString("ERROR");
	            	}else{
	            		returnRecs.add(new Recommendation(json_data.getString("recid"), 
	            				json_data.getString("message"), json_data.getString("sender"),
	            				json_data.getString("recipient"), json_data.getString("dateSent"),
	            				json_data.getString("isGroupReceipient").equals("1"), json_data.getString("appid")));
		            }
	            }
		    }catch(JSONException e){
		    		
		    }
		   	  
		    return returnRecs;
	}		

}